package com.example.steveaoblogdemo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.steveaoblogdemo.entity.User;
import com.example.steveaoblogdemo.mapper.UserMapper;
import com.example.steveaoblogdemo.service.UserService;
import com.example.steveaoblogdemo.utils.JwtUtil;
import com.example.steveaoblogdemo.utils.Result;
import com.example.steveaoblogdemo.utils.SaltUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    UserMapper userMapper;

    public Result<?> login(User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        User res = userMapper.selectOne(wrapper.eq(User::getUsername,user.getUsername()).eq(User::getPassword,user.getPassword()).eq(User::getRole,1));
        if (res == null){
            return new Result<>(400,"用户名或密码错误",user);
        }else {
            String token = JwtUtil.createToken(user.getUsername());
            user.setToken(token);
            return new Result<>(200,"登录成功",user);
        }
    }

    public Result<?> adminLog(String username,String password){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("role",1).eq("username",username).eq("status",0);
        User user = userMapper.selectOne(wrapper);
        if (user != null){
            Integer salt = user.getSalt();
            String pwd = DigestUtils.md5Hex(password+salt);
            if (pwd.equals(user.getPassword())){
                String token = JwtUtil.createToken(user.getUsername());
                user.setToken(token);
                return new Result<>(200,"登录成功",user);
            }
            return new Result<>(400,"登录失败",null);
        }
        return new Result<>(400,"登录失败",null);
    }

    public Result<?> log(String username,String password){
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("role",0).eq("username",username).eq("status",0);
        User user = userMapper.selectOne(wrapper);
        if (user != null){
            Integer salt = user.getSalt();
            String pwd = DigestUtils.md5Hex(password+salt);
            if (pwd.equals(user.getPassword())){
                String token = JwtUtil.createToken(user.getUsername());
                user.setToken(token);
                return new Result<>(200,"登录成功",user);
            }
            return new Result<>(400,"登录失败",null);
        }
    return new Result<>(400,"登录失败",null);
    }

    public Result<?> queryById(Integer id){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        User user = userMapper.selectOne(wrapper);
        if (user!=null){
            return new Result<>(200,"查询成功",user);
        }
        return new Result<>(400,"查询失败",null);
    }

    public Result<?> queryUser(Integer pageNum,Integer pageSize,String search){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)){
            wrapper.eq("status",0).like("username",search);
        }
        wrapper.eq("status",0);
        IPage<User> userIPage = userMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return new Result<>(200,"查询成功",userIPage);
    }

    public int updateUser(String username,String nickname,String password,String avatar,String sex,String email,String resume,Integer salt,Integer recommend,Integer role,Integer id){
        int x = userMapper.updateUser(username,nickname,password,avatar,sex,email,resume,salt,recommend,role,id);
        if (x>0){
            return 1;
        }
        return 0;
    }

    public int addUser(Integer id,String username,String nickname,String password,String avatar,String sex,String email,String resume,Integer salt,Integer recommend,Integer role){
        int x = userMapper.addUser(id,username,nickname,password,avatar,sex,email,resume,salt,recommend,role);
        if (x>0){
            return 0;
        }
        return 1;
    }

    public Result<?> deleteUser(Integer id){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("status",1);
        int result = userMapper.update(null,wrapper);
        if (result>0){
            return new Result<>(200,"删除成功",result);
        }
        return new Result<>(400,"删除失败",null);
    }

    public Result<?> queryByName(String username){
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if (user==null){
            return new Result<>(200,"可以注册",null);
        }
        return new Result<>(400,"用户名重复，请重新输入",user);
    }

    //查询普通用户邮箱是否存在，管理员的邮箱和普通用户分开
    public Result<?> queryByEmail(String email){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email).eq("role",0);
        User user = userMapper.selectOne(wrapper);
        if (user==null){
            return new Result<>(200,"可以注册",null);
        }
        return new Result<>(400,"邮箱重复，请重新输入",user);
    }

    public Result<?> forgetMyPassword(String email,String password){
        UpdateWrapper<User> wrapper = new UpdateWrapper();
        Integer saltForOne = SaltUtil.createSalt();
        String pwd = DigestUtils.md5Hex(password+saltForOne);
        wrapper.eq("email",email).set("salt",saltForOne).set("password",pwd);
        int result = userMapper.update(null,wrapper);
        if (result>0){
            return new Result<>(200,"密码修改成功",result);
        }
        return new Result<>(400,"密码修改失败",null);
    }

    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return JwtUtil.checkToken(token);
    }

    public List<User> getUserInfo(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<User> userList = userMapper.selectList(queryWrapper);
        return userList;
    }

}
